//
//  ViewController.swift
//  flutter_app
//
//  Created by dfsx6 on 2021/11/22.
//

import UIKit
import Flutter
import FlutterPluginRegistrant
import pigeon_common

class ViewController: UIViewController {

    let config = FLTGlobalConfig.init()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        print(#file, #function)
        
    }


    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
//        let engines = (UIApplication.shared.delegate as! AppDelegate).engines
        let engines = FlutterManager.shared.engines

        let engine = engines.makeEngine(withEntrypoint: "message", libraryURI: nil)

//        let engine = (UIApplication.shared.delegate as! AppDelegate).engine
        
//        engine.run()
//        GeneratedPluginRegistrant.register(with: engine)
//        HostGlobalConfigApiSetup(engine.binaryMessenger, self)
//        HostMessageApiSetup(engine.binaryMessenger, self)
//        config.baseUrl = "http://192.168.8.30:8989"
//        config.token = "e84a01037dbe424e9a87d86cc8b1bd6f"
        engine.run()
        GeneratedPluginRegistrant.register(with: engine)
        HostGlobalConfigApiSetup(engine.binaryMessenger, HostGlobalConfigPlugin.init())
        HostMessageApiSetup(engine.binaryMessenger, HostMessagePlugin.init())
//        let messageApi = FLTMessagApi.init(binaryMessenger: engine.binaryMessenger)
//        messageApi.queryMessageTemplates { (_) in
//            print("sssss2222")
//        }
        let vc = FlutterSubViewController.init(engine: engine, nibName: nil, bundle: nil)
        vc.modalPresentationStyle = .fullScreen
        present(vc, animated: true, completion: nil)
    }
}

class HostGlobalConfigPlugin: NSObject, HostGlobalConfigApi {
    func getGlobalConfigWithError(_ error: AutoreleasingUnsafeMutablePointer<FlutterError?>) -> FLTGlobalConfig? {
        let config = FLTGlobalConfig.init()
        config.baseUrl = "http://apitest.baview.cn:39191"
        config.token = "c61a6045975d4632bc0d5ed4afa7e8b4"
        return config
    }
}

extension ViewController: HostGlobalConfigApi {
    func getGlobalConfigWithError(_ error: AutoreleasingUnsafeMutablePointer<FlutterError?>) -> FLTGlobalConfig? {
        config.baseUrl = "http://apitest.baview.cn:39191"
        config.token = "c61a6045975d4632bc0d5ed4afa7e8b4"
        return config
    }
    
    
}

class HostMessagePlugin: NSObject, HostMessageApi {
    func clickMessageMessage(_ message: FLTMessage, error: AutoreleasingUnsafeMutablePointer<FlutterError?>) {
        print("ddddd")
    }
    
    
}

extension ViewController: HostMessageApi {
    func clickMessageMessage(_ message: FLTMessage, error: AutoreleasingUnsafeMutablePointer<FlutterError?>) {
        print("ddddd")
    }
    
}

class FlutterSubViewController: FlutterViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
//        let messageApi = FLTMessagApi.init(binaryMessenger: self.engine!.binaryMessenger)
//        messageApi.queryMessageTemplates { (_) in
//            print("sssss")
//        }
    }
}


class FlutterManager: NSObject {
    private override init() {
        super.init()
    }
    
    static let shared = FlutterManager.init()
    
    let engines = FlutterEngineGroup.init(name: "my flutter engine group", project: nil)

}
